package com.aternity.geolocations.transregions;

import au.com.bytecode.opencsv.CSVReader;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Table;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
 * @author Daniel Orr
 */
public class MaxMindRegions {

    // row: country ISO 3166 code, column: state code (FIPS for non US and ISO 3166 for US), value: state name
    private Table<String, String, String> stateData = HashBasedTable.create();

    public MaxMindRegions() {

    }

    public void load(File f) throws IOException {
        try(CSVReader csvReader = new CSVReader(new FileReader(f))){
            String[] line;
            while( (line = csvReader.readNext()) != null){
                stateData.put(line[0], line[1], line[2]);
            }
        }
    }

    public Set<String> getIso3166AllCountries(){
        return stateData.columnKeySet();
    }

    public Map<String, String> getCountryData(String iso3166){
        if("BF".equals(iso3166)){
            System.out.println();
        }
        Map<String, String> row = stateData.row(iso3166);
        //BiMap<String, String> data = HashBiMap.create(row);
        return row;
    }
}
